Working with operator[] and operator=

Posted by calebthorne on Stack Overflow See other posts from Stack Overflow or by calebthorne
Published on 2010-05-04T15:50:16Z Indexed on 2010/05/04 15:58 UTC
Read the original article Hit count: 350

Filed under:
|

Given a simple class that overloads the '[ ]' operator:

class A
{
  public:
    int operator[](int p_index)
    {
       return a[p_index];
    }

  private:
    int a[5];
};

I would like to accomplish the following:

void main()
{
   A Aobject;

   Aobject[0] = 1;  // Problem here
}

How can I overload the assignment '=' operator in this case to work with the '[ ]' operator?

© Stack Overflow or respective owner

Related posts about c++

Related posts about operator-overloading